<template>
  <div id="app">
    <h1>📝 待办事项 TodoList</h1>
    <div class="input-area">
      <input v-model="newTodo" @keyup.enter="addTodo" placeholder="输入新的待办事项" />
      <button @click="addTodo">添加</button>
    </div>
    <div class="filters">
      <button @click="filter = 'all'" :class="{ active: filter === 'all' }">全部</button>
      <button @click="filter = 'active'" :class="{ active: filter === 'active' }">未完成</button>
      <button @click="filter = 'completed'" :class="{ active: filter === 'completed' }">已完成</button>
    </div>
    <p v-if="filteredTodos.length === 0" class="empty-tip">这里什么都没有呢～快去添加吧！(｡･ω･｡)</p>
    <transition-group name="list" tag="ul" class="todo-list">
      <li v-for="todo in filteredTodos" :key="todo.id">
        <input type="checkbox" v-model="todo.done" @change="saveTodos" />
        <span :class="{ done: todo.done }">{{ todo.text }}</span>
        <button class="delete-btn" @click="deleteTodo(todo.id)">删除</button>
      </li>
    </transition-group>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'

const newTodo = ref('')
const todos = ref<{ id: number; text: string; done: boolean }[]>([])
const filter = ref('all')

const STORAGE_KEY = 'vue-todolist'

function loadTodos() {
  const storedTodos = localStorage.getItem(STORAGE_KEY)
  if (storedTodos) {
    todos.value = JSON.parse(storedTodos)
  }
}

function saveTodos() {
  localStorage.setItem(STORAGE_KEY, JSON.stringify(todos.value))
}

function addTodo() {
  const text = newTodo.value.trim()
  if (text) {
    todos.value.push({ id: Date.now(), text, done: false })
    saveTodos()
    newTodo.value = ''
  }
}

function deleteTodo(id: number) {
  todos.value = todos.value.filter(todo => todo.id !== id)
  saveTodos()
}

const filteredTodos = computed(() => {
  if (filter.value === 'active') {
    return todos.value.filter(todo => !todo.done)
  } else if (filter.value === 'completed') {
    return todos.value.filter(todo => todo.done)
  }
  return todos.value
})

watch(todos, () => {
  saveTodos()
}, { deep: true })

onMounted(() => {
  loadTodos()
  console.log('萌萌TodoList已加载～ฅ^•ﻌ•^ฅ')
})
</script>

<style scoped>
/* 背景色和字体 */
body {
  margin: 0;
  background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%);
  font-family: 'Comic Sans MS', 'Chalkboard', cursive;
  min-height: 100vh;
}

/* 中心卡片 */
#app {
  max-width: 600px;
  margin: 50px auto;
  background: white;
  padding: 30px;
  border-radius: 20px;
  box-shadow: 0 8px 16px rgba(0,0,0,0.2);
  text-align: center;
}

h1 {
  color: #ff6ec7;
  margin-bottom: 20px;
  font-weight: bold;
}

/* 输入区域 */
.input-area {
  display: flex;
  margin-bottom: 20px;
}

.input-area input {
  flex: 1;
  padding: 10px;
  font-size: 16px;
  border: 2px solid #ffb6b9;
  border-radius: 8px;
  transition: border-color 0.3s;
}

.input-area input:focus {
  border-color: #ff6ec7;
  outline: none;
}

.input-area button {
  margin-left: 10px;
  padding: 10px 20px;
  background-color: #ff6ec7;
  color: white;
  font-weight: bold;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.input-area button:hover {
  background-color: #ff4fa3;
}

/* 筛选按钮 */
.filters {
  margin-bottom: 20px;
}

.filters button {
  margin: 0 5px;
  background: #f3d6ff;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s;
}

.filters button:hover {
  transform: translateY(-2px);
}

.filters button.active {
  background: #ff6ec7;
  color: white;
}

/* 空提示 */
.empty-tip {
  color: #888;
  margin: 20px 0;
}

/* 待办列表 */
.todo-list {
  list-style: none;
  padding: 0;
}

.todo-list li {
  display: flex;
  align-items: center;
  background: #fafafa;
  margin-bottom: 10px;
  padding: 10px 15px;
  border-radius: 10px;
  transition: all 0.3s ease;
}

.todo-list li:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.todo-list input[type="checkbox"] {
  margin-right: 10px;
  cursor: pointer;
}

.todo-list span {
  flex: 1;
  text-align: left;
  word-break: break-word;
}

.todo-list span.done {
  text-decoration: line-through;
  color: #aaa;
}

.delete-btn {
  background-color: #ff4d4f;
  color: white;
  border: none;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.delete-btn:hover {
  background-color: #d9363e;
}

/* 动画效果 */
.list-enter-active, .list-leave-active {
  transition: all 0.4s;
}

.list-enter-from, .list-leave-to {
  opacity: 0;
  transform: translateY(20px);
}

@media (max-width: 640px) {
  #app {
    margin: 20px;
    padding: 15px;
  }
}
</style>